package com.audaque.springboot.foshanupload.web.servletdemo.controller;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wangyan01
 */

@Slf4j
@Controller
@RequestMapping("view")
public class ViewController {
    @GetMapping("/status/{status}")
    public ModelAndView status(@PathVariable("status") String status) {
        ModelAndView mv = new ModelAndView();
        //不能用error,因为会与springboot的冲突
        mv.setViewName(status);
        return mv;
    }

    @GetMapping("/scope")
    public ModelAndView scope() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("scope");
        return mv;
    }


    @RequestMapping("index-jsp")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("remoteUser", "admin");
        mv.setViewName("index");
        return mv;
    }

    @RequestMapping("sse-jsp")
    public ModelAndView sse() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("remoteUser", "admin");
        mv.setViewName("sse");
        return mv;
    }


    @GetMapping("books-ftl")
    public ModelAndView booksftl() {
        List<Book> books = new ArrayList<>();
        Book b1 = new Book();
        b1.setId(1);
        b1.setAuthor("罗贯中");
        b1.setName("三国演义");
        Book b2 = new Book();
        b2.setId(2);
        b2.setAuthor("曹雪芹");
        b2.setName("红楼梦");
        books.add(b1);
        books.add(b2);
        ModelAndView mv = new ModelAndView();
        mv.addObject("books", books);
        mv.setViewName("books");
        return mv;

    }

    @GetMapping("books-html")
    public ModelAndView bookshtml() {
        List<Book> books = new ArrayList<>();
        Book b1 = new Book();
        b1.setId(1);
        b1.setAuthor("罗贯中");
        b1.setName("三国演义");
        Book b2 = new Book();
        b2.setId(2);
        b2.setAuthor("曹雪芹");
        b2.setName("红楼梦");
        books.add(b1);
        books.add(b2);
        ModelAndView mv = new ModelAndView();
        mv.addObject("books", books);
        mv.setViewName("books");
        return mv;

    }


    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    class Book {
        private Integer id;
        private String name;
        private String author;
    }

}

